package com.digitalchina.lxnsy.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.DataDetailActivity;
import com.digitalchina.lxnsy.api.BusinessMerchant;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.GoodsVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.GoodsAllHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：商户全部商品
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2017/1/11 14:50
 * 修改备注：
 * ================================================
 */
public class GoodsAllFragment extends BaseFragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private List<GoodsVO> goodsVOList = new ArrayList<>();
    private RecyclerArrayAdapter adapter;

    private int page = 1;
    private int limit = 10;

    private String merchantId;

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_goods_all);
    }

    @Override
    public void initMember() {

        Bundle bundle = getArguments();
        if (null != bundle)
            merchantId = bundle.getString("merchantId");
        else
            merchantId = "";

        // 设置merchantId
//        merchantId = "33e5d761-1ae1-4b18-b8e9-1813b87d55b6";

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(getActivity(), false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new GoodsAllHolder(parent);
            }
        });

        onRefresh();

    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                GoodsVO goodsVO = (GoodsVO) adapter.getAllData().get(position);
                if (null != goodsVO.getId())
                    GotoUtil.gotoActivity(getActivity(), DataDetailActivity.class, "productId", goodsVO.getId());
            }
        });

    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_ALL_GOODS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_ALL_GOODS_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else {
                            if (recyclerView == null)
                                return;
                            recyclerView.showEmpty();
                        }
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        goodsVOList = FastJsonUtil.getListBean(object.toString(), "body", "commodityInfoList", GoodsVO.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (goodsVOList != null) {
            if (goodsVOList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(goodsVOList);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessMerchant.getAllGoods(getActivity(), merchantId, String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @Override
    public void onLoadMore() {
        page++;
        BusinessMerchant.getAllGoods(getActivity(), merchantId, String.valueOf(page), String.valueOf(limit), mHandler);
    }
}
